﻿using System;
using Xunit;

namespace Leetcode.N0003.V2
{
    /// <summary>
    /// 滑动窗口
    /// </summary>
    public class Solution
    {
        public int LengthOfLongestSubstring(string s)
        {
            var flag = new int[256];
            var from = 0;
            var to = -1;
            var max = 0;

            for (int i = 0; i < flag.Length; i++)
            {
                flag[i] = -1;
            }

            for (int i = 0; i < s.Length; i++)
            {
                if (flag[s[i]] >= from)
                {
                    max = Math.Max(max, to - from + 1);
                    from = flag[s[i]];
                }
                else
                {
                    to = i;
                }
                flag[s[i]] = i + 1;
            }
            return Math.Max(max, to - from + 1);

        }
    }

}
